package com.simulation.ctrl;

import com.simulation.entity.GpioDTO;
import com.simulation.entity.GpioUpdateDTO;
import com.simulation.service.ExecutorService;
import com.simulation.utils.Consts;
import lombok.extern.slf4j.Slf4j;
import org.anarres.qemu.qapi.api.QomSetCommand;
import org.anarres.qemu.qapi.common.QApiConnection;
import org.anarres.qemu.qapi.common.QApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
 * @author zhangxn
 * @date 2022/12/4  11:22
 */
@Slf4j
@RestController
@RequestMapping("/simulation")
public class SimulationController {

    @Autowired
    private ExecutorService executorService;

    @Value("${qemu.ip}")
    private String ip;

    @Value("${qemu.qmp.port}")
    private String qmpPort;



    @GetMapping("/startNc")
    public String startNc(String commond){
        executorService.execLinuxCommod(commond, Consts.TYPE_COMMON);
        return "success";
    }

    @GetMapping("/stopNc")
    public String stopNc(String commond){
        executorService.execLinuxCommodSync("kill -9 `ps -ef | grep nc | grep -v grep | awk '{print $2}'`");
        return "success";
    }

    @GetMapping("/startBash")
    public String startBash(String commond){
        executorService.execLinuxCommodBash("bash /root/qemu-stm32/start.sh");
        return "success";
    }

    @GetMapping("/stopBash")
    public String stopBash(String commond){
        executorService.execLinuxCommodBash("kill -9 `ps -ef | grep qemu-system-arm | grep -v grep | awk '{print $2}'`");
        return "success";
    }

    @GetMapping("/execGpio")
    public String execGpio(String commond){
        QApiConnection connection = null;
        try {
            connection = new QApiConnection(new InetSocketAddress(ip, 24));
            System.out.println(connection.call(new QomSetCommand("/machine/unattached/device[0]/GPIOD", "input[0]",false)));
            return "success";
        } catch (IOException | QApiException e) {
            e.printStackTrace();
        }
        return "callback({\"state\":\"success\"})";
    }

    /**
     * 参数传递请求
     * @param proper
     * @param value
     * @return
     */
    @GetMapping("/exec-gpio")
    public String execGpioParam(@RequestParam("proper") String proper,@RequestParam("portInt") Integer portInt,@RequestParam("value") Boolean value){
        QApiConnection connection = null;
        try {
            connection = new QApiConnection(new InetSocketAddress(ip, Integer.valueOf(qmpPort)));
            System.out.println(connection.call(new QomSetCommand("/machine/unattached/device[0]/GPIOD", proper + "[" + portInt + "]",value)));
            return "success";
        } catch (IOException | QApiException e) {
            e.printStackTrace();
        }
        return "callback({\"state\":\"success\"})";
    }


    @GetMapping("/start-all")
    public String startAll(String commond){
        try {
            log.info(">>>>>>>>>>>{}",ip);
            executorService.startAll(commond);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "callback({\"state\":\"success\"})";
    }

    @GetMapping("/debug-all")
    public String debugAll(String commond){
        try {
            log.info(">>>>>>>>>>>{}",ip);
            executorService.debugAll(commond);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "callback({\"state\":\"success\"})";
    }

    @GetMapping("/start-all-test")
    public String startAllTest(String commond){
        try {
            executorService.startAllTest(commond);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "callback({\"state\":\"success\"})";
    }


    @GetMapping("/stop-all")
    public String stopAll(){
        executorService.stopAll();
        return "callback({\"state\":\"success\"})";
    }

    /**
     * 参数传递请求
     * @param gpioDTO
     * @return
     */
    @GetMapping("/exec-gpio-time")
    public String execGpioTimeParam(GpioDTO gpioDTO){
        try {
            QApiConnection connection = new QApiConnection(new InetSocketAddress(ip, Integer.valueOf(qmpPort)));
            System.out.println(connection.call(new QomSetCommand("/machine/unattached/device[0]/ADC", "adc1[3]", gpioDTO.getPropertiesValue())));
//            System.out.println(connection.call(new QomSetCommand("/machine/unattached/device[0]/" + gpioDTO.getGpio(), gpioDTO.getProperties(), gpioDTO.getPropertiesValue())));
//            System.out.println(connection.call(new QomSetCommand("/machine/unattached/device[0]/" + gpioDTO.getGpio(), gpioDTO.getType()+"["+gpioDTO.getPort()+"]",gpioDTO.getGpValue())));
            connection.close();
            return "callback({\"state\":\"success\"})";
        } catch (IOException | QApiException e) {
            e.printStackTrace();
        }
        return "callback({\"state\":\"success\"})";
    }

    /**
     * 参数传递请求
     * @param updateDTO
     * @return
     */
    @GetMapping("/exec-gpio-update")
    public String execGpioTimeUpdate(GpioUpdateDTO updateDTO){
        try {
            QApiConnection connection = new QApiConnection(new InetSocketAddress(ip, Integer.valueOf(qmpPort)));
            System.out.println(connection.call(new QomSetCommand("/machine/unattached/device[0]/" + updateDTO.getGpio(), updateDTO.getProperties(), updateDTO.getPropertiesValue())));
            System.out.println(connection.call(new QomSetCommand("/machine/unattached/device[0]/" + updateDTO.getGpio(), updateDTO.getType()+"["+updateDTO.getPort()+"]",updateDTO.getGpValue())));
            connection.close();
            return "callback({\"state\":\"success\"})";
        } catch (IOException | QApiException e) {
            e.printStackTrace();
        }
        return "callback({\"state\":\"success\"})";
    }

}
